package org.example.Servlet.PersonServlet;

import org.example.Dao.PersonDao;
import org.example.entity.Person;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/person/delPerson")
public class DelPersonServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        PersonDao personDao = new PersonDao();
        HttpSession session = req.getSession();
        int pid = Integer.parseInt(req.getParameter("pid"));
        //删除
        try {
            int count = personDao.delPersonByPid(pid);
            if (count > 0) {
                // 重新获取人员集合
                List<Person> personList = personDao.getNormalPersonList();
                System.out.println("personList ==> " + personList.toString());
                session.setAttribute("personList", personList);
                session.setAttribute("delPersonMsg", "");
            } else {
                session.setAttribute("delPersonMsg", "删除人员信息失败");
            }
            req.getRequestDispatcher("/views/admin/index.jsp").forward(req, resp);
        } catch (Exception e) {
            System.out.println("del person error ==> " + e.getMessage());
            req.getRequestDispatcher("/views/admin/index.jsp").forward(req,resp);
        }
    }
}
